
//
//  StringMessageEncoder.m
//  PinBan
//
//  Created by kenta.yao on 15/11/4.
//  Copyright © 2015年 centling. All rights reserved.
//

#import "StringMessageEncoder.h"
#import "NSObject+Ext.h"

/*
 int tmp1 = 1;
 int tmp2 = CFSwapInt32BigToHost(tmp1);
 NSData* data1 = [NSData dataWithBytes:&tmp1 length:sizeof(tmp1)];
 NSData* data2 = [NSData dataWithBytes:&tmp2 length:sizeof(tmp2)];
 
 */

@implementation StringMessageEncoder

+ (NSMutableData *)encodeWith:(NettyMessage *)nettyMessage{

    NSMutableData *mutableData = [NSMutableData data];
    if (!(nettyMessage == nil || nettyMessage.nettyMessageHeader == nil)){
    
        int crcCode = nettyMessage.nettyMessageHeader.crcCode;
        NSData *crcCodeData = [NSData dataWithBytes:&crcCode length:sizeof(crcCode)];
        int length = nettyMessage.nettyMessageHeader.length;
//        int length = 92;
        int swapedLength = CFSwapInt32BigToHost(length);
        NSData *lengthData = [NSData dataWithBytes:&swapedLength length:sizeof(swapedLength)];
        long sessionID = nettyMessage.nettyMessageHeader.sessionID;
        NSData *sessionIDData = [NSData dataWithBytes:&sessionID length:sizeof(sessionID)];
        Byte type = nettyMessage.nettyMessageHeader.type;
        NSData *typeData = [NSData dataWithBytes:&type length:sizeof(type)];
        Byte priority = nettyMessage.nettyMessageHeader.priority;
        NSData *priorityData = [NSData dataWithBytes:&priority length:sizeof(priority)];
//        int attachment = sizeof(nettyMessage.nettyMessageHeader.attachment);
        int attachmentInt = 0;
        int swapedAttachmentInt = CFSwapInt32BigToHost(attachmentInt);
        NSData *attachmentData = [NSData dataWithBytes:&swapedAttachmentInt length:sizeof(swapedAttachmentInt)];
        [mutableData appendData:crcCodeData];
        [mutableData appendData:lengthData];
        [mutableData appendData:sessionIDData];
        [mutableData appendData:typeData];
        [mutableData appendData:priorityData];
        [mutableData appendData:attachmentData];
    }

    if (nettyMessage.nettyMessageBody != nil) {

        NSString *jsonString = nil;
        NSError *error;

        NSDictionary *dic = [nettyMessage.nettyMessageBody getDictionaryFromObject_Ext:nettyMessage.nettyMessageBody];
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];

        if (! jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
 
        NSData *bodyBytes = [jsonString dataUsingEncoding: NSUTF8StringEncoding];

        int length = [[NSString stringWithFormat:@"%lu",(unsigned long)bodyBytes.length] intValue];
        int swapLength = CFSwapInt32BigToHost(length);
        NSData *bodyLengthData = [NSData dataWithBytes:&swapLength length:sizeof(swapLength)];
        
        
        [mutableData appendData:bodyLengthData];
        [mutableData appendData:bodyBytes];
        
    } 
    else{
        int a = 0;
        NSData *aData = [NSData dataWithBytes:&a length:sizeof(a)];
        [mutableData appendData:aData];
    }
    int length = [[NSString stringWithFormat:@"%lu",(unsigned long)(mutableData.length - 8)] intValue];
    int swapedLength = CFSwapInt32BigToHost(length);
    [mutableData replaceBytesInRange:NSMakeRange(4,4) withBytes:&swapedLength length:4];
//    [mutableData replaceBytesInRange:NSMakeRange(4, 7) withBytes:&swapedLength];
    return mutableData;
}


@end
